package com.lobov.entities;

import java.io.Serializable;

import org.apache.commons.lang.ArrayUtils;

public class ContainerOfSnips extends Timer implements Serializable {

	private static final long serialVersionUID = 1L;

	public ContainerOfSnips(int numOfSnips, int snipsXLength) {
		this.snips = new Snip[numOfSnips];
		this.snipsXLength = snipsXLength;
	}

	private Snip[] snips = null;
	private String name;
	private int snipsXLength = 0;

	public Snip[] getSnips() {
		return snips;
	}

	public void setSnips(Snip[] snips) {
		this.snips = snips;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Snip getSnipByIndex(int i) {
		return this.snips[i];
	}
	
	public void setSnipByIndex(Snip snip, int i) {
		this.snips[i] = snip;
	}

	public int getSnipsXLength() {
		return snipsXLength;
	}
	
	public Matrix toMatrix() throws Exception{
		int matrixSize = 0; 
		for (Snip s: snips){
			if(s == null){
				throw new Exception("Ошибка! Не весь контейнер лоскутов заполнен!");
			}
			matrixSize+=s.getValues().length;
		}
		int xLength = snipsXLength;
		int yLength = matrixSize/xLength;
		Matrix matrix = new Matrix(xLength, yLength);
		double[] values = null;
		int[] xs = null;
		int[] ys = null;
		
		for (Snip s: snips){
			values = ArrayUtils.addAll(values, s.getValues());
			xs = ArrayUtils.addAll(xs, s.getXs());
			ys = ArrayUtils.addAll(ys, s.getYs());
			
			
		}
		matrix.setValues(values);
		matrix.setXs(xs);
		matrix.setYs(ys);
		
		return matrix;
	}
	@Override
	public String toString() {
		
		try {
			return this.toMatrix().toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ERROR";
	}
	
}
